package com.gh.ok.utils

import android.content.Intent
import android.net.Uri
import androidx.fragment.app.FragmentActivity
import com.ok.common.base.AppContext
import com.permissionx.guolindev.PermissionX

object PermissionUtils {

    /**
     * Launch the application's details settings.
     */
    fun launchAppDetailsSettings() {
        val intent = Intent("android.settings.APPLICATION_DETAILS_SETTINGS")
        intent.data = Uri.parse("package:" + AppContext.packageName)
        AppContext.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
    }

    fun apply(activity: FragmentActivity, callBack: RequestCallBack, permissions: List<String>) {
        PermissionX.init(activity).permissions(permissions).onExplainRequestReason { scope, deniedList ->
            val message = "PermissionX需要您同意以下权限才能正常使用"
            scope.showRequestReasonDialog(deniedList, message, "Allow", "Deny")
        }.onForwardToSettings { scope, deniedList ->
//            scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中手动开启权限", "我已明白")
        }.request { allGranted, grantedList, deniedList ->
            if (allGranted) {
                callBack.granted()
            } else {
                callBack.denied(deniedList)
            }
        }
    }
}

interface RequestCallBack {
    fun granted()
    fun denied(deniedList: List<String?>?)
}